<?php
// 应用公共文件
function now(){
    return date('Y-m-d H:i:s');
}
function doCurlPostRequest($url = '',Array $data = array())
{
    $data_string = json_encode($data,JSON_UNESCAPED_UNICODE);
    // $data_string = $data;
    $curl_con = curl_init();
    curl_setopt($curl_con, CURLOPT_URL,$url);
    curl_setopt($curl_con, CURLOPT_HEADER, false);
    curl_setopt($curl_con, CURLOPT_POST, true);
    curl_setopt($curl_con, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl_con, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($curl_con, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
    );
    curl_setopt($curl_con, CURLOPT_POSTFIELDS, $data_string);
    $res = curl_exec($curl_con);
    curl_close($curl_con);
    return $res;
}
function curl($url, $header,$is_post = true,$data = []){
    // $useragent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36';
    // 创建一个cURL资源
    $ch = curl_init();
    // 设置cURL选项
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, $is_post);
    curl_setopt($ch, CURLOPT_URL, $url);          // 设置目标网页的URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将返回的内容作为字符串返回，而不是直接输出
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁止 cURL 验证对等证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//是否检测服务器的域名与证书上的是否一致
    curl_setopt($ch, CURLOPT_ENCODING, "" );
    curl_setopt($ch, CURLOPT_AUTOREFERER, true );
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5 );
    curl_setopt($ch, CURLOPT_TIMEOUT, 5 );
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5 );
    if(!empty($data)){
        $post = http_build_query($data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    // curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    // 执行cURL请求
    $response = curl_exec($ch);
    // 关闭cURL资源
    curl_close($ch);
    // 检查是否有错误发生
    if ($response === false) {
        $error = curl_error($ch);
        return ['error' => $error];
    } else {
        // 处理返回的内容
        return $response;
    }
}
function numberToChineseUppercase($number) {
    $cnums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
    $cnyunits = array("分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟");
    $grees = array("", "拾", "佰", "仟");

    $num = round($number, 2); // 保留两位小数
    $num_arr = explode(".", $num);
    $int_num = $num_arr[0];
    $dec_num = isset($num_arr[1]) ? $num_arr[1] : "00";

    $dec_num = substr(str_pad($dec_num, 2, "0"), 0, 2); // 确保小数部分有两位

    $chinese_num = "";

    // 处理整数部分
    $len = strlen($int_num);
    $last_zero = false;
    for ($i = 0; $i < $len; $i++) {
        $n = $int_num[$i];
        $p = $len - $i - 1;
        $q = intval($p / 4);
        $m = $p % 4;

        if ($n == "0") {
            if (($last_zero === false) && ($m != 0) && ($p != $len - 1)) {
                $chinese_num .= $cnums[0];
            }
            $last_zero = true;
        } else {
            $chinese_num .= $cnums[$n] . $grees[$m];
            $last_zero = false;
        }

        if ($m == 0) {
            $chinese_num .= $cnyunits[$q * 4 + 3];
        }
    }

    $chinese_num = rtrim($chinese_num, "零");
    $chinese_num = preg_replace("/零+/", "零", $chinese_num);
    $chinese_num = preg_replace("/零(万|亿|兆)/", "$1", $chinese_num);
    $chinese_num = preg_replace("/亿万/", "亿", $chinese_num);
    $chinese_num = preg_replace("/零+/", "零", $chinese_num);
    $chinese_num = preg_replace("/零$/", "", $chinese_num);

    if (empty($chinese_num)) {
        $chinese_num = "零";
    }

    // 处理小数部分
    $dec_part = "";
    if ($dec_num[0] != "0") {
        $dec_part .= $cnums[$dec_num[0]] . $cnyunits[1];
    }
    if ($dec_num[1] != "0") {
        $dec_part .= $cnums[$dec_num[1]] . $cnyunits[0];
    }

    if (!empty($dec_part)) {
        $chinese_num .= "点" . $dec_part;
    } else {
        $chinese_num .= "整";
    }

    return $chinese_num . "元";
}

function send_email($email,$title,$body){
    $message = new \Swift_Message($title);
    $message = $message->setFrom('ppf1788@sina.com');
    $message = $message->setTo($email);
    $message = $message->setBody($body,'text/html' );
    $transport = new \Swift_SmtpTransport('smtp.sina.com', 465,'ssl');
    $transport = $transport->setUsername('ppf1788@sina.com');
    $transport = $transport->setPassword('14c1f2883d0b344e');
    $mailer = new \Swift_Mailer($transport);
    $mailer->send($message);
}